<html><head>
      <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
   <title>The File Language</title><link rel="stylesheet" type="text/css" href="eclipse_book.css"><meta name="generator" content="DocBook XSL Stylesheets V1.77.1"><meta name="description" content="The file language is an extension to the simple language, not an independent language in its own right. But the file language extension can only be used in conjunction with File or FTP endpoints."><link rel="home" href="index.html" title="Red Hat Fuse Tooling"><link rel="up" href="RiderExLang.html" title="Part&nbsp;III.&nbsp;Expression and Predicates Languages"><link rel="prev" href="EL.html" title="EL"><link rel="next" href="Groovy.html" title="Groovy"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="chapter"><div class="titlepage"><div><div><h2 class="title"><a name="File"></a>The File Language</h2></div><div><div class="abstract"><p>The file language is an extension to the simple language, not an independent
language in its own right. But the file language extension can only be used in
conjunction with File or FTP endpoints.</p></div></div></div></div><div class="toc"><p><b>Table of Contents</b></p><dl><dt><span class="section"><a href="File.html#File-When">When to Use the File Language</a></span></dt><dt><span class="section"><a href="File.html#File-Variables">File Variables</a></span></dt><dt><span class="section"><a href="File.html#File-Examples">Examples</a></span></dt></dl></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="File-When"></a>When to Use the File Language</h2></div></div></div><div class="simplesect"><div class="titlepage"><div><div><h3 class="title"><a name="topic-33497"></a>Overview</h3></div></div></div><p>The file language is an extension to the simple language which is not always
available. You can use it under the following circumstances:</p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><a class="link" href="File.html#File-When-InFileOrFtp" title="In a File or FTP consumer endpoint">In a File or FTP consumer endpoint</a>.</li><li class="listitem"><a class="link" href="File.html#File-When-OnExchanges" title="On exchanges created by a File or FTP consumer">On exchanges created by a File or FTP consumer</a>.</li></ul></div><div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><table border="0" summary="Note"><tr><td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="imagesdb/note.png"></td><th align="left">Note</th></tr><tr><td align="left" valign="top"><p>The escape character, <code class="literal">\</code>, is not available in the file
language.</p></td></tr></table></div></div><div class="simplesect"><div class="titlepage"><div><div><h3 class="title"><a name="File-When-InFileOrFtp"></a>In a File or FTP consumer endpoint</h3></div></div></div><p>There are several URI options that you can set on a File or FTP consumer endpoint,
which take a file language expression as their value. For example, in a File
consumer endpoint URI you can set the <code class="literal">fileName</code>, <code class="literal">move</code>,
<code class="literal">preMove</code>, <code class="literal">moveFailed</code>, and <code class="literal">sortBy</code> options
using a file expression.</p><p>In a File consumer endpoint, the <code class="literal">fileName</code> option acts as a filter,
determining which file will actually be read from the starting directory. If a plain
text string is specified (for example, <code class="literal">fileName=report.txt</code>), the File
consumer reads the same file each time it is updated. You can make this option more
dynamic, however, by specifying a simple expression. For example, you could use a
counter bean to select a different file each time the File consumer polls the
starting directory, as follows:</p><pre class="screen">file://target/filelanguage/bean/?fileName=${bean:counter.next}.txt&amp;delete=true</pre><p>Where the <code class="literal">${bean:counter.next}</code> expression invokes the
<code class="literal">next()</code> method on the bean registered under the ID,
<code class="literal">counter</code>.</p><p>The <code class="literal">move</code> option is used to move files to a backup location after then
have been read by a File consumer endpoint. For example, the following endpoint
moves files to a backup directory, after they have been processed:</p><pre class="screen">file://target/filelanguage/?move=backup/${date:now:yyyyMMdd}/${file:name.noext}.bak&amp;recursive=false</pre><p>Where the <code class="literal">${file:name.noext}.bak</code> expression modifies the original
file name, replacing the file extension with <code class="literal">.bak</code>.</p><p>You can use the <code class="literal">sortBy</code> option to specify the order in which file
should be processed. For example, to process files according to the alphabetical
order of their file name, you could use the following File consumer endpoint:</p><pre class="screen">file://target/filelanguage/?sortBy=file:name</pre><p>To process file according to the order in which they were last modified, you could
use the following File consumer endpoint:</p><pre class="screen">file://target/filelanguage/?sortBy=file:modified</pre><p>You can reverse the order by adding the <code class="literal">reverse:</code> prefix&#8201;&#8212;&#8201;for
example:</p><pre class="screen">file://target/filelanguage/?sortBy=reverse:file:modified</pre></div><div class="simplesect"><div class="titlepage"><div><div><h3 class="title"><a name="File-When-OnExchanges"></a>On exchanges created by a File or FTP consumer</h3></div></div></div><p>When an exchange originates from a File or FTP consumer endpoint, it is possible
to apply file language expressions to the exchange throughout the route (as long as
the original message headers are not erased). For example, you could define a
content-based router, which routes messages according to their file extension, as
follows:</p><pre class="screen">&lt;from uri="file://input/orders"/&gt;
&lt;choice&gt;
  &lt;when&gt;
    &lt;simple&gt;${file:ext} == 'txt'&lt;/simple&gt;
    &lt;to uri="bean:orderService?method=handleTextFiles"/&gt;
  &lt;/when&gt;
  &lt;when&gt;
    &lt;simple&gt;${file:ext} == 'xml'&lt;/simple&gt;
    &lt;to uri="bean:orderService?method=handleXmlFiles"/&gt;
  &lt;/when&gt;
  &lt;otherwise&gt;
    &lt;to uri="bean:orderService?method=handleOtherFiles"/&gt;
  &lt;/otherwise&gt;
&lt;/choice&gt;</pre></div></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="File-Variables"></a>File Variables</h2></div></div></div><div class="simplesect"><div class="titlepage"><div><div><h3 class="title"><a name="topic-33500"></a>Overview</h3></div></div></div><p>File variables can be used whenever a route starts with a File or FTP consumer
endpoint, which implies that the underlying message body is of
<code class="literal">java.io.File</code> type. The file variables enable you to access various
parts of the file pathname, almost as if you were invoking the methods of the
<code class="literal">java.io.File</code> class (in fact, the file language extracts the
information it needs from message headers that have been set by the File or FTP
endpoint).</p></div><div class="simplesect"><div class="titlepage"><div><div><h3 class="title"><a name="topic-33501"></a>Starting directory</h3></div></div></div><p>Some of file variables return paths that are defined relative to a
<span class="emphasis"><em>starting directory</em></span>, which is just the directory that is
specified in the File or FTP endpoint. For example, the following File consumer
endpoint has the starting directory, <code class="literal">./filetransfer</code> (a relative
path):</p><pre class="screen">file:filetransfer</pre><p>The following FTP consumer endpoint has the starting directory,
<code class="literal">./ftptransfer</code> (a relative path):</p><pre class="screen">ftp://myhost:2100/ftptransfer</pre></div><div class="simplesect"><div class="titlepage"><div><div><h3 class="title"><a name="topic-33502"></a>Naming convention of file variables</h3></div></div></div><p>In general, the file variables are named after corresponding methods on the
<code class="literal">java.io.File</code> class. For example, the <code class="literal">file:absolute</code>
variable gives the value that would be returned by the
<code class="literal">java.io.File.getAbsolute()</code> method.</p><div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><table border="0" summary="Note"><tr><td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="imagesdb/note.png"></td><th align="left">Note</th></tr><tr><td align="left" valign="top"><p>This naming convention is not strictly followed, however. For example, there
is <span class="strong"><strong>no</strong></span> such method as
<code class="literal">java.io.File.getSize()</code>.</p></td></tr></table></div></div><div class="simplesect"><div class="titlepage"><div><div><h3 class="title"><a name="topic-33503"></a>Table of variables</h3></div></div></div><p><a class="xref" href="File.html#File-Variables-TableVFTFL" title="Table&nbsp;6.&nbsp;Variables for the File Language">Table&nbsp;6, &#8220;Variables for the File Language&#8221;</a> shows all of the variable supported by
the file language.</p><div class="table"><a name="File-Variables-TableVFTFL"></a><p class="title"><b>Table&nbsp;6.&nbsp;Variables for the File Language</b></p><div class="table-contents"><table summary="Variables for the File Language" border="1"><colgroup><col width="33%" class="col_1"><col width="33%" class="col_2"><col width="34%" class="col_3"></colgroup><thead><tr><th align="left" valign="top">Variable</th><th align="left" valign="top">Type</th><th align="left" valign="top">Description</th></tr></thead><tbody><tr><td align="left" valign="top"><p><code class="literal">file:name</code></p></td><td align="left" valign="top"><p><code class="literal">String</code></p></td><td align="left" valign="top"><p>The pathname relative to the starting directory.</p></td></tr><tr><td align="left" valign="top"><p><code class="literal">file:name.ext</code></p></td><td align="left" valign="top"><p><code class="literal">String</code></p></td><td align="left" valign="top"><p>The file extension (characters following the last <code class="literal">.</code>
                                character in the pathname). Supports file extensions with multiple dots, for example, .tar.gz.</p></td></tr><tr><td align="left" valign="top"><p><code class="literal">file:name.ext.single</code></p></td><td align="left" valign="top"><p><code class="literal">String</code></p></td><td align="left" valign="top"><p>The file extension (characters following the last <code class="literal">.</code>
                                character in the pathname). If the file extension has mutiple dots, then this expression only returns the last part.</p></td></tr><tr><td align="left" valign="top"><p><code class="literal">file:name.noext</code></p></td><td align="left" valign="top"><p><code class="literal">String</code></p></td><td align="left" valign="top"><p>The pathname relative to the starting directory, omitting the
                                file extension.</p></td></tr><tr><td align="left" valign="top"><p><code class="literal">file:name.noext.single</code></p></td><td align="left" valign="top"><p><code class="literal">String</code></p></td><td align="left" valign="top"><p>The pathname relative to the starting directory, omitting the
                                file extension. If the file extension has multiple dots, then this expression strips only the last part, and keep the others.</p></td></tr><tr><td align="left" valign="top"><p><code class="literal">file:onlyname</code></p></td><td align="left" valign="top"><p><code class="literal">String</code></p></td><td align="left" valign="top"><p>The final segment of the pathname. That is, the file name without
                                the parent directory path.</p></td></tr><tr><td align="left" valign="top"><p><code class="literal">file:onlyname.noext</code></p></td><td align="left" valign="top"><p><code class="literal">String</code></p></td><td align="left" valign="top"><p>The final segment of the pathname, omitting the file
                                extension.</p></td></tr><tr><td align="left" valign="top"><p><code class="literal">file:onlyname.noext.single</code></p></td><td align="left" valign="top"><p><code class="literal">String</code></p></td><td align="left" valign="top"><p>The final segment of the pathname, omitting the file
                                extension. If the file extension has multiple dots, then this expression strips only the last part, and keep the others.</p></td></tr><tr><td align="left" valign="top"><p><code class="literal">file:ext</code></p></td><td align="left" valign="top"><p><code class="literal">String</code></p></td><td align="left" valign="top"><p>The file extension (same as <code class="literal">file:name.ext</code>).</p></td></tr><tr><td align="left" valign="top"><p><code class="literal">file:parent</code></p></td><td align="left" valign="top"><p><code class="literal">String</code></p></td><td align="left" valign="top"><p>The pathname of the parent directory, including the starting
                                directory in the path.</p></td></tr><tr><td align="left" valign="top"><p><code class="literal">file:path</code></p></td><td align="left" valign="top"><p><code class="literal">String</code></p></td><td align="left" valign="top"><p>The file pathname, including the starting directory in the
                                path.</p></td></tr><tr><td align="left" valign="top"><p><code class="literal">file:absolute</code></p></td><td align="left" valign="top"><p><code class="literal">Boolean</code></p></td><td align="left" valign="top"><p><code class="literal">true</code>, if the starting directory was specified as an
                                absolute path; <code class="literal">false</code>, otherwise.</p></td></tr><tr><td align="left" valign="top"><p><code class="literal">file:absolute.path</code></p></td><td align="left" valign="top"><p><code class="literal">String</code></p></td><td align="left" valign="top"><p>The absolute pathname of the file.</p></td></tr><tr><td align="left" valign="top"><p><code class="literal">file:length</code></p></td><td align="left" valign="top"><p><code class="literal">Long</code></p></td><td align="left" valign="top"><p>The size of the referenced file.</p></td></tr><tr><td align="left" valign="top"><p><code class="literal">file:size</code></p></td><td align="left" valign="top"><p><code class="literal">Long</code></p></td><td align="left" valign="top"><p><span class="strong"><strong>Same as <code class="literal">file:length</code>.</strong></span></p></td></tr><tr><td align="left" valign="top"><p><code class="literal">file:modified</code></p></td><td align="left" valign="top"><p><code class="literal">java.util.Date</code></p></td><td align="left" valign="top"><p>Date last modified.</p></td></tr></tbody></table></div></div><br class="table-break"></div></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="File-Examples"></a>Examples</h2></div></div></div><div class="simplesect"><div class="titlepage"><div><div><h3 class="title"><a name="topic-33504"></a>Relative pathname</h3></div></div></div><p>Consider a File consumer endpoint, where the starting directory is specified as a
<span class="strong"><strong>relative pathname</strong></span>. For example, the following File endpoint
has the starting directory, <code class="literal">./filelanguage</code>:</p><pre class="screen">file://filelanguage</pre><p>Now, while scanning the <code class="literal">filelanguage</code> directory, suppose that the
endpoint has just consumed the following file:</p><pre class="screen">./filelanguage/test/hello.txt</pre><p>And, finally, assume that the <code class="literal">filelanguage</code> directory itself has the
following absolute location:</p><pre class="screen">/workspace/camel/camel-core/target/filelanguage</pre><p>Given the preceding scenario, the file language variables return the following
values, when applied to the current exchange:</p><div class="informaltable"><table border="1"><colgroup><col width="50%" class="col_1"><col width="50%" class="col_2"></colgroup><thead><tr><th align="left" valign="top">Expression</th><th align="left" valign="top">Result</th></tr></thead><tbody><tr><td align="left" valign="top"><p><code class="literal">file:name</code></p></td><td align="left" valign="top"><p><code class="literal">test/hello.txt</code></p></td></tr><tr><td align="left" valign="top"><p><code class="literal">file:name.ext</code></p></td><td align="left" valign="top"><p><code class="literal">txt</code></p></td></tr><tr><td align="left" valign="top"><p><code class="literal">file:name.noext</code></p></td><td align="left" valign="top"><p><code class="literal">test/hello</code></p></td></tr><tr><td align="left" valign="top"><p><code class="literal">file:onlyname</code></p></td><td align="left" valign="top"><p><code class="literal">hello.txt</code></p></td></tr><tr><td align="left" valign="top"><p><code class="literal">file:onlyname.noext</code></p></td><td align="left" valign="top"><p><code class="literal">hello</code></p></td></tr><tr><td align="left" valign="top"><p><code class="literal">file:ext</code></p></td><td align="left" valign="top"><p><code class="literal">txt</code></p></td></tr><tr><td align="left" valign="top"><p><code class="literal">file:parent</code></p></td><td align="left" valign="top"><p><code class="literal">filelanguage/test</code></p></td></tr><tr><td align="left" valign="top"><p><code class="literal">file:path</code></p></td><td align="left" valign="top"><p><code class="literal">filelanguage/test/hello.txt</code></p></td></tr><tr><td align="left" valign="top"><p><code class="literal">file:absolute</code></p></td><td align="left" valign="top"><p><code class="literal">false</code></p></td></tr><tr><td align="left" valign="top"><p><code class="literal">file:absolute.path</code></p></td><td align="left" valign="top"><p><code class="literal">/workspace/camel/camel-core/target/filelanguage/test/hello.txt</code></p></td></tr></tbody></table></div></div><div class="simplesect"><div class="titlepage"><div><div><h3 class="title"><a name="topic-33505"></a>Absolute pathname</h3></div></div></div><p>Consider a File consumer endpoint, where the starting directory is specified as an
<span class="strong"><strong>absolute pathname</strong></span>. For example, the following File endpoint
has the starting directory,
<code class="literal">/workspace/camel/camel-core/target/filelanguage</code>:</p><pre class="screen">file:///workspace/camel/camel-core/target/filelanguage</pre><p>Now, while scanning the <code class="literal">filelanguage</code> directory, suppose that the
endpoint has just consumed the following file:</p><pre class="screen">./filelanguage/test/hello.txt</pre><p>Given the preceding scenario, the file language variables return the following
values, when applied to the current exchange:</p><div class="informaltable"><table border="1"><colgroup><col width="50%" class="col_1"><col width="50%" class="col_2"></colgroup><thead><tr><th align="left" valign="top">Expression</th><th align="left" valign="top">Result</th></tr></thead><tbody><tr><td align="left" valign="top"><p><code class="literal">file:name</code></p></td><td align="left" valign="top"><p><code class="literal">test/hello.txt</code></p></td></tr><tr><td align="left" valign="top"><p><code class="literal">file:name.ext</code></p></td><td align="left" valign="top"><p><code class="literal">txt</code></p></td></tr><tr><td align="left" valign="top"><p><code class="literal">file:name.noext</code></p></td><td align="left" valign="top"><p><code class="literal">test/hello</code></p></td></tr><tr><td align="left" valign="top"><p><code class="literal">file:onlyname</code></p></td><td align="left" valign="top"><p><code class="literal">hello.txt</code></p></td></tr><tr><td align="left" valign="top"><p><code class="literal">file:onlyname.noext</code></p></td><td align="left" valign="top"><p><code class="literal">hello</code></p></td></tr><tr><td align="left" valign="top"><p><code class="literal">file:ext</code></p></td><td align="left" valign="top"><p><code class="literal">txt</code></p></td></tr><tr><td align="left" valign="top"><p><code class="literal">file:parent</code></p></td><td align="left" valign="top"><p><code class="literal">/workspace/camel/camel-core/target/filelanguage/test</code></p></td></tr><tr><td align="left" valign="top"><p><code class="literal">file:path</code></p></td><td align="left" valign="top"><p><code class="literal">/workspace/camel/camel-core/target/filelanguage/test/hello.txt</code></p></td></tr><tr><td align="left" valign="top"><p><code class="literal">file:absolute</code></p></td><td align="left" valign="top"><p><code class="literal">true</code></p></td></tr><tr><td align="left" valign="top"><p><code class="literal">file:absolute.path</code></p></td><td align="left" valign="top"><p><code class="literal">/workspace/camel/camel-core/target/filelanguage/test/hello.txt</code></p></td></tr></tbody></table></div></div></div></div></body></html>